package com.ehualu.esresult.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/**
 * @Author: 吴敬超
 * @Date: 2020/7/13 11:59
 */
public class MysqlUtil {

    private static final Logger LOG = LoggerFactory.getLogger(MysqlUtil.class);


    /**
     * @param carnum 修改id
     */
    public void updateRuningcarnum(Integer carnum) {


        GetDBConnection getDBConnection = new GetDBConnection();
        Connection conn = getDBConnection.getdbconn();


        Integer norunnum = 0;


        Statement st = null;
        ResultSet rs = null;
        try {
            st = conn.createStatement();

            String sqlcarnum = String.format("UPDATE runingcarnum set carnum = %s WHERE id = 'carnum'", carnum);

            String sqlallcarnum = "SELECT t.carnum as allcarsum FROM runingcarnum t WHERE t.id ='allcarnum'";


            LOG.info("sqlcarnum is " + sqlcarnum);

            LOG.info("sqlallcarnum is " + sqlallcarnum);

            st.executeUpdate(sqlcarnum);

            rs = st.executeQuery(sqlallcarnum);


            while (rs.next()) {

                Integer sumnum = rs.getInt("allcarsum");

                LOG.info("sumnum is " + sumnum);

                norunnum = sumnum - carnum;

            }
            LOG.info("norunnum is " + norunnum);
            String sqlcarnotrun = String.format("UPDATE runingcarnum set carnum = %s WHERE id = 'norun'", norunnum);
            LOG.info("sqlcarnotrun is " + sqlcarnotrun);

            st.executeUpdate(sqlcarnotrun);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            DBCloseUtills dbCloseUtills = new DBCloseUtills();
            dbCloseUtills.release(conn, st, rs);


        }


    }

    /**
     * @param idnum        修改id
     * @param responsetime 平均响应时间
     * @param ordernum     订单完成率
     * @param peopnum      平均乘坐人数
     */
    public void updateQuality(String idnum, String responsetime, String ordernum, String peopnum) {


        GetDBConnection getDBConnection = new GetDBConnection();
        Connection conn = getDBConnection.getdbconn();


        Statement st = null;
        ResultSet rs = null;
        try {
            st = conn.createStatement();

            String sqlupdate = String.format("UPDATE quality set responsetime = %s,ordernum=%s,peopnum=%s WHERE idnum = '%s'", responsetime, ordernum, peopnum, idnum);


            LOG.info("修改sql sqlupdate is " + sqlupdate);


            st.executeUpdate(sqlupdate);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            DBCloseUtills dbCloseUtills = new DBCloseUtills();
            dbCloseUtills.release(conn, st, rs);


        }


    }


    public void updateRuntimes(String id, String countnum) {


        GetDBConnection getDBConnection = new GetDBConnection();
        Connection conn = getDBConnection.getdbconn();


        Statement st = null;
        ResultSet rs = null;
        try {
            st = conn.createStatement();

            String sqlupdate = String.format("UPDATE runtime set countnum = %s WHERE id = '%s'", countnum, id);


            LOG.info("修改sql sqlupdate is " + sqlupdate);


            st.executeUpdate(sqlupdate);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            DBCloseUtills dbCloseUtills = new DBCloseUtills();
            dbCloseUtills.release(conn, st, rs);


        }


    }


/*

    public static void main(String[] args) {


        MysqlUtil mysqlUtil = new MysqlUtil();
        mysqlUtil.updateRuningcarnum(9);

    }
*/

}
